package SaveServlet.manager;

import com.lyq.bean.Manager;
import com.lyq.bean.service.M_Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/RemoveManager_Servlet")
public class RemoveManager_Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public RemoveManager_Servlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取所有管理员信息
            List<Manager> managers = new ArrayList<>();
            managers = new M_Service().seekAll_M(managers);

            request.setAttribute("managers", managers);
            request.getRequestDispatcher("/jsp/manager/DeleteManager.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "获取管理员列表失败: " + e.getMessage());
            request.getRequestDispatcher("/jsp/manager/DeleteManager.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String managerName = request.getParameter("managerName");

            if (managerName == null || managerName.trim().isEmpty()) {
                request.setAttribute("errorMessage", "管理员用户名不能为空");
                doGet(request, response);
                return;
            }

            // 执行删除操作
            Boolean result = new M_Service().remove_M(managerName);

            if (result) {
                request.setAttribute("successMessage", "删除管理员成功");
            } else {
                request.setAttribute("errorMessage", "删除管理员失败");
            }

            // 重新加载管理员列表
            doGet(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "删除管理员异常: " + e.getMessage());
            doGet(request, response);
        }
    }
}